package com.auxgroup.hr.front.commons.utils;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.auxgroup.hr.front.commons.exception.FrontException;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.exception.FrontException;

/** 
 * @author lvliuhao 
 * @version 创建时间：2018年7月20日 下午4:37:42 
 * 类说明 
 */
@SuppressWarnings("unchecked")
public class WebServiceUtils {

	static int socketTimeout = 30000;// 请求超时时间
	static int connectTimeout = 30000;// 传输超时时间
	private static final Logger logger = LoggerFactory
			.getLogger(WebServiceClient.class);
	
	
	/**
	 * 使用SOAP1.1发送消息
	 * 
	 * @param postUrl
	 * @param soapXml
	 * @param soapAction
	 * @return
	 */
	public static String doPostSoap(String postUrl, String soapXml,
			String soapAction) {
		logger.info("WebService request soapaction:{}, soapXml:{}",soapAction,soapXml);
		String retStr = "";
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpPost httpPost = new HttpPost(postUrl);
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(socketTimeout)
				.setConnectTimeout(connectTimeout).build();
		httpPost.setConfig(requestConfig);
		try {
			httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
			httpPost.setHeader("SOAPAction", soapAction);
			StringEntity data = new StringEntity(soapXml,
					Charset.forName("UTF-8"));
			httpPost.setEntity(data);
			CloseableHttpResponse response = closeableHttpClient
					.execute(httpPost);
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				// 打印响应内容
				retStr = EntityUtils.toString(httpEntity, "UTF-8");
				logger.info("WebService response :{}" , retStr);
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
			logger.error("WebService error", e);
		}
		return retStr;
	}
	
	
	/**
	 * 解析返回的soapxml
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：");
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	
	
	/**
	 * 解析返回的sendMsgXMl
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleSendMsgRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_MESSAGE_WS_RES").element(respEleName).element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	
	
	/**
	 * 解析返回的evaPointXMl
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleEvaPointRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_EVA_POINT_RSP").element("MsgData").element("Transaction").element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	/**
	 * 解析返回的calcPointXMl
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleCalcPointRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_EP_EVALUATE_S").element("MsgData").element("Transaction").element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	/**
	 * 解析返回的epStatusXMl
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleEpStatustRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_EP_STATUS_S").element("MsgData").element("Transaction").element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	
	/**
	 * 解析返回的rankWS内容
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleRankWSRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_EP_RANK_WS_RES").element("C_EP_RANK_RES").element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	/**
	 * 解析返回的rankWS内容
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleWfWSRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_EP_WF_WS_RES").element("C_EP_WF_RES").element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	
	/**
	 * 解析返回的rankWS内容
	 * @param respXml
	 * @return
	 */
	public static Map<String, String> handleIntrvwReqRespXml(String respXml,String respEleName){
		Map<String, String> resultMap = new HashMap<String, String>();
		logger.info("开始解析返回xml：{}",respXml);
		if(StringUtils.isNotBlank(respXml)){
			try {
				Document doc = DocumentHelper.parseText(respXml);
				Element root = doc.getRootElement();
				Element body = (Element)root.elementIterator().next();
				if(body!=null){
					Element resp = body.element("C_EP_INTRVW_WS_RES").element("C_EP_INTRVW_RES").element(respEleName);
					if(resp==null){
						return null;
					}
					Iterator<Element> resultEles = resp.elementIterator();
					while (resultEles.hasNext()) {
						Element result = resultEles.next();
						resultMap.put(result.getName(), result.getTextTrim());
					}
				}
			} catch (DocumentException e) {
				logger.error("解析返回xml出错：",e);
				throw new FrontException(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
				
			}
		}
		logger.info("解析xml结果：{}",resultMap);
		return resultMap;
	}
	
	
}
 